package com.xiyuan.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xiyuan.constant.MessageConstant;
import com.xiyuan.constant.UserConstant;
import com.xiyuan.mapper.UserMapper;
import com.xiyuan.pojo.entity.User;
import com.xiyuan.pojo.result.PageBean;
import com.xiyuan.service.UserService;
import com.xiyuan.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;


    /**
     * 根据账号查询用户信息
     * @param username
     * @return
     */
    @Override
    public User findByUserName(String username) {
        return userMapper.findUserByUsername(username);
    }

    /**
     * 根据id查询用户信息
     * @param id
     * @return
     */
    @Override
    public User findUserById(Integer id) {
        return userMapper.findUserById(id);
    }

    @Override
    public int updateUser(User user) {
        Map map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        User userById = userMapper.findUserById(id);
        if (user.getId() == id){
            int result = userMapper.updateUser(user);
            if (result == 0){
                throw new RuntimeException(MessageConstant.UPDATE_FAIL);
            }
            return result;
        }else if (userById.getRole() == 0 || userById.getRole() == 1){//管理员操作
            int result = userMapper.updateUser(user);
            if (result == 0){
                throw new RuntimeException(MessageConstant.UPDATE_FAIL);
            }
            return result;
        }else {
            throw new RuntimeException(MessageConstant.LOGIN_FAIL);
        }
    }

    @Override
    public void register(User user) {
        userMapper.insert(user);
    }

    /**
     * 获取用户列表
     * @return
     */
    @Override
    public PageBean<User> getList(User user) {
        Map map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        User userById = userMapper.findUserById(id);
        //判断权限查询对应用户列表
        if (userById.getRole() == 2){
            throw new RuntimeException(MessageConstant.PERMISSION_ERROR);
        }
        user.setRole(userById.getRole());
        //创建PageBean对象
        PageBean pb = new PageBean<>();
        //开启分页查询
        PageHelper.startPage(user.getPageNum(), user.getPageSize());
        List<User> list = userMapper.getList(user);
        Page<User> page = (Page<User>) list;
        pb.setTotal(page.getTotal());
        pb.setItems(page.getResult());
        return pb;
    }

    /**
     * 添加管理员
     * @param user
     */
    @Override
    public void addAdmin(User user) {
        Map map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        User userById = userMapper.findUserById(id);
        if (userById.getRole() == 0){
            //管理员默认密码
            user.setPassword(DigestUtils.md5DigestAsHex(UserConstant.DEFAULT_PASSWORD.getBytes()));
            user.setRole(UserConstant.ROLE_MANAGER);
            userMapper.insert(user);
        }else {
            throw new RuntimeException(MessageConstant.PERMISSION_ERROR);
        }
    }
}
